package Gui;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import Command.MainCommand;

import com.cloudgarden.resource.SWTResourceManager;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class viewFiles extends org.eclipse.swt.widgets.Composite {

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}
	
	
	private static String selected;
	private Button open;
	private Button[] file;
	private Composite composite1;
	private Button cancel;
	static MainCommand mainCommand;
	private static Shell shell;
	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
//	public static void main(String[] args) {
//		showGUI();
//	}
	
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI(String slc, MainCommand mainCmd) {
		mainCommand=mainCmd;
		selected=slc;
		Display display = Display.getDefault();
		shell = new Shell(display);
		viewFiles inst = new viewFiles(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public viewFiles(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			{
				composite1 = new Composite(this, SWT.NONE);
				FormLayout composite1Layout = new FormLayout();
				FormData composite1LData = new FormData();
				composite1LData.left =  new FormAttachment(0, 1000, 0);
				composite1LData.top =  new FormAttachment(0, 1000, 0);
				composite1LData.width = 564;
				composite1LData.height = 353;
				composite1.setLayoutData(composite1LData);
				composite1.setLayout(composite1Layout);
				composite1.setBackground(SWTResourceManager.getColor(255, 206, 206));
				{
					String[] fileStr=mainCommand.ShoeFilesByTag(selected);
					
					int i, size= fileStr.length;
					file=new Button[size];
					
					for (i=0; i<size; i++)
					{
						file[i] = new Button(composite1, SWT.RADIO | SWT.LEFT);
						FormData fileLData = new FormData();
						fileLData.left =  new FormAttachment(50, 1000, 23);
						fileLData.top =  new FormAttachment(50, 1000, 17);
						fileLData.width = 487;
						fileLData.height = 19;
						fileLData.left =  new FormAttachment(50, 1000, 23);
						fileLData.top =  new FormAttachment(50+i*50, 1000, 17);
						file[i].setLayoutData(fileLData);
						file[i].setText(fileStr[i]);
						file[i].setBackground(SWTResourceManager.getColor(255, 128, 255));
						
					}
					
					
					
					
					
				}
				{
					open = new Button(composite1, SWT.PUSH | SWT.CENTER);
					FormData openLData = new FormData();
					openLData.width = 63;
					openLData.height = 27;
					openLData.left =  new FormAttachment(632, 1000, 0);
					openLData.right =  new FormAttachment(743, 1000, 0);
					openLData.top =  new FormAttachment(871, 1000, 0);
					openLData.bottom =  new FormAttachment(947, 1000, 0);
					open.setLayoutData(openLData);
					open.setText("open");
					open.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							int i, index=0,size=file.length;
							for (i=0; i<size; i++)
							{
								if(file[i].getSelection())
								{
									index=i;
									break;
								}
							}
							String fileStr=file[index].getText();
							
							Desktop desktop = null;
                            File file2 = new File(fileStr);
                            if (Desktop.isDesktopSupported()) {
                                    desktop = Desktop.getDesktop();
                            }
                            try
                            {
                                    desktop.open(file2.getParentFile());
                            }
                            catch (IOException e){
                            	
                            }
//							Runtime myCommand = java.lang.Runtime.getRuntime();
//							try {
//								
//								myCommand.exec("cmd /c \"" + fileStr + "\"");
//							} catch (IOException e) {
//								e.printStackTrace();
//							}
						}
					});
				}
				{
					cancel = new Button(composite1, SWT.PUSH | SWT.CENTER);
					FormData cancelLData = new FormData();
					cancelLData.width = 65;
					cancelLData.height = 27;
					cancelLData.left =  new FormAttachment(814, 1000, 0);
					cancelLData.right =  new FormAttachment(929, 1000, 0);
					cancelLData.top =  new FormAttachment(871, 1000, 0);
					cancelLData.bottom =  new FormAttachment(947, 1000, 0);
					cancel.setLayoutData(cancelLData);
					cancel.setText("cancel");
					cancel.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							shell.dispose();
						}
					});
				}
			}
			this.layout();
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
